package com.bdqn.t350.controller;

import com.bdqn.t350.pojo.SysUser;
import com.bdqn.t350.service.SysUserService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet("/ListServlet")
public class ListServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println("doget.....");
        //初始化spring容器
        // 从application作用域（ServletContext）获得spring容器
        //方式1： 手动从作用域获取 getAttribute返回的Object类型 需要强转
        ApplicationContext apppApplicationContext = (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        //方式2：通过工具获取
        //ApplicationContext apppApplicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        String[] beanDefinitionNames = apppApplicationContext.getBeanDefinitionNames();
        Arrays.sort(beanDefinitionNames);
        System.out.println(Arrays.asList(beanDefinitionNames));
        SysUserService sysUserServiceImp = (SysUserService) apppApplicationContext.getBean("sysUserServiceImp");
        List<SysUser> listBiz = sysUserServiceImp.getListBiz();
        request.setAttribute("list", listBiz);
        request.getRequestDispatcher("list.jsp").forward(request, response);

    }
}
